#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-08-15 18:23:39
# @Author  : shitao.tommy (hero007asd@gmail.com)
# @Link    : http://example.org
# @Version : $Id$

from tornado.web import UIModule

class AdminSidebarModule(UIModule):
    def render(self):
        return self.render_string('widget/admin_sidebar_new.html')

class FooterModule(UIModule):
    def render(self):
        return self.render_string('layout/bottom.html')

class HeadModule(UIModule):
    def render(self):
        return self.render_string('layout/head.html')

class FloatModule(UIModule):
    def render(self):
        return self.render_string('widget/float.html')

class Paginate(UIModule):
    '''分页uimodule
    '''
    def render(self, total, page, pagesize=15):
        uri = self.request.uri.split('?')

        if len(uri) > 1:
            args = "&".join(filter(lambda ai: not ai.startswith('page'), uri[1].split('&')))
            if args != "":
                uri = "?".join([uri[0], args])

        if isinstance(uri, list):
            uri = uri[0]

        pages = 1
        if total > 0:
            pages = total / pagesize
        page = int(page)

        if total % pagesize > 0:
            pages = pages + 1

        if uri.find("?") > -1:
            uri = uri + "&page="
        else:
            uri = uri + "?page="

        prepage = 1
        if page - 1 > 1:
            prepage = page - 1

        nextpage = pages
        if page + 1 < pages:
            nextpage = page + 1

        start_num = page
        end_num = pages - page

        return self.render_string('widget/admin-page.html', total = total, pagesize = pagesize, startnum = start_num, endnum=end_num, pages = pages, page = page, prepage = prepage, nextpage = nextpage, url = uri,)


